﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BeerManagementSystem
{
    public class EmulatedCardReader : ICardReader
    {
        bool _ready = false;
        bool _accepting = false;
        private EmulatedCardReaderInput _input;
        LogWriter log;

        public EmulatedCardReader()
        {
            this.log = log;
            _ready = true;
            _input = new EmulatedCardReaderInput(this);
            _input.Show();
        }

        public void AcceptNextCard()
        {
            _accepting = true;
        }

        public void Read(int EmulatedIdentity)
        {
            if (_accepting)
            {
                _accepting = false;

                switch (EmulatedIdentity)
                {
                    case 68:
                        //reset barrel
                        BarrelReset(this, null);
                        break;
                    case 30:
                        //reset last transaction
                        ResetLastTransaction(this, null);
                        break;
                    case 2:
                        //nächste karte läd die übernächste auf ein bier ein
                        Einladen(this, null);
                        break;
                    default:
                        CardRead(this, new CardReadEventArgs(EmulatedIdentity));
                        break;
                }
            }
        }

        public void Invalid()
        {
            CardInvalid(this, null);
        }

        public bool IsReady()
        {
            return _ready;
        }
        public void close()
        {
            _input.Close();
            //log.WriteMessage("EmulatedCardReader: shutdown");
        }

        public event EventHandler<CardReadEventArgs> CardRead;

        public event EventHandler CardInvalid;

        public event EventHandler BarrelReset;

        public event EventHandler ResetLastTransaction;

        public event EventHandler Einladen;


    }
}
